package com.fer.service.impl;

import com.fer.mapper.GoodsMapper;
import com.fer.model.Goods;
import com.fer.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * GoodsService实现类
 * 实现关于商品的功能
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    //查找全部商品
    @Override
    public List<Goods> selectAllGoods(Integer shop_id) {
        return goodsMapper.selectAllGoods(shop_id);
    }

    //通过商品的id查询商品返回
    @Override
    public Goods selectGood(Integer goods_id) {
        return goodsMapper.selectGood(goods_id);
    }

    //通过商品的name查询商品返回
    @Override
    public List<Goods> selectGoodByName(Integer shop_id,String goods_name) {
        return goodsMapper.selectGoodByName(shop_id,goods_name);
    }

    //增加一个商品信息
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public void insertGood(Goods goods) {
//        goods.set_sale(false);
        goodsMapper.insertGood(goods);
//        System.out.println(goods.getGoods_id()+"添加成功");
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public void deleteGood(Integer goods_id) {
        goodsMapper.deleteGood(goods_id);
        System.out.println("删除成功");
    }

    @Override
    public boolean updateGood(Goods goods) {
        int result = goodsMapper.updateGood(goods);
        if(result==-1){
            return false;
        }
        return true;
    }

}
